package edu.zqu.collegeField.framework.activiti.service;

import org.activiti.editor.language.json.converter.util.CollectionUtils;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.task.Task;

import java.util.ArrayList;
import java.util.List;


public class ActivitiTaskQuery extends ActivitiBase {

    public static List<Task> taskQuery(String processKey, String assignee) {
        /**
         * 3.根据流程定义的key,负责人assignee来实现当前用户的任务列表查询
         */
        List<Task> taskList = taskService.createTaskQuery()
                .processDefinitionKey(processKey)
                .taskAssignee(assignee)
                .list();
        /**
         * 4.任务列表的展示
         */
        taskList.forEach((item) -> {
            System.out.println("流程实例ID" + item.getProcessInstanceId());
            System.out.println("任务ID" + item.getId());
            System.out.println("任务负责人" + item.getAssignee());
        });
        return taskList;
    }

    //查询当前任务 也就是通过流程实例ID查找出当前的实例流程进行到哪一步了
    public static Task getTask(String processInstanceId) {
        Task task = taskService.createTaskQuery()// 创建查询对象
                .processInstanceId(processInstanceId)// 通过流程实例id来查询当前任务
                .singleResult();// 获取单个查询结果
         /*   if (task == null) {
                System.out.println("流程已结束");
                System.out.println("流程实例ID:" + processInstanceId);
                System.out.println("*****************************************************************************");
            }*/
        System.out.println("任务ID:" + task.getId());
        System.out.println("任务名称:" + task.getName());
        System.out.println("任务的创建时间:" + task.getCreateTime());
        System.out.println("任务的办理人:" + task.getAssignee());
        System.out.println("流程实例ID：" + task.getProcessInstanceId());
        System.out.println("执行对象ID:" + task.getExecutionId());
        System.out.println("流程定义ID:" + task.getProcessDefinitionId());
        System.out.println("*****************************************************************************");
        return task;
    }

    //查询当前所有任务
    public static List<Task> listTask(String processInstanceId) {

     /*   if (StringUtils.isEmpty(processInstanceId)) {
            return "param error";
        }*/

        List<Task> taskList = taskService.createTaskQuery()// 创建查询对象
                .processInstanceId(processInstanceId)// 通过流程实例id来查询当前任务
                .list();
        if (CollectionUtils.isEmpty(taskList)) {
            System.out.println("流程已结束");
            System.out.println("流程实例ID:" + processInstanceId);
            System.out.println("*****************************************************************************");
        }

        taskList.forEach(task -> {
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务名称:" + task.getName());
            System.out.println("任务的创建时间:" + task.getCreateTime());
            System.out.println("任务的办理人:" + task.getAssignee());
            System.out.println("流程实例ID：" + task.getProcessInstanceId());
            System.out.println("执行对象ID:" + task.getExecutionId());
            System.out.println("流程定义ID:" + task.getProcessDefinitionId());
            System.out.println("*****************************************************************************");
        });

        return taskList;
    }

    //查询用户的当前需要审批的任务
    public static List<Task> myAudit(String assignee) {
        List<Task> taskList = taskService.createTaskQuery().taskCandidateOrAssigned(assignee)
                .orderByTaskCreateTime().desc().list();
        return taskList;
    }

    //查询用户的已经办理但流程还没有结束的任务
    public static List<HistoricTaskInstance> queryDoneTasks(String assignee) {
        List<HistoricTaskInstance> taskList = historyService.createHistoricTaskInstanceQuery()
                .taskAssignee(assignee)
                .finished()
                .list();
        List<HistoricTaskInstance> taskRemove = new ArrayList<>();
        for (HistoricTaskInstance task : taskList) {
            boolean state = ActivitiRuntime.processState(task.getProcessInstanceId());
            if (state) {
                taskRemove.add(task);
            }
        }
        taskList.removeAll(taskRemove);
        for (HistoricTaskInstance task : taskList) {
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务名称:" + task.getName());
            System.out.println("任务的创建时间:" + task.getCreateTime());
            System.out.println("任务的办理人:" + task.getAssignee());
            System.out.println("流程实例ID：" + task.getProcessInstanceId());
            System.out.println("执行对象ID:" + task.getExecutionId());
            System.out.println("流程定义ID:" + task.getProcessDefinitionId());
            System.out.println("*****************************************************************************");
        }
        return taskList;
    }

    //查询用户的已经办理流程已经结束的历史任务的任务
    public static List<HistoricTaskInstance> queryHistoryTasks(String assignee) {
        List<HistoricTaskInstance> taskList = historyService.createHistoricTaskInstanceQuery()
                .taskAssignee(assignee)
                .finished()
                .list();
        List<HistoricTaskInstance> taskRemove = new ArrayList<>();
        for (HistoricTaskInstance task : taskList) {
            boolean state = ActivitiRuntime.processState(task.getProcessInstanceId());
            if (state) {
                taskRemove.add(task);
            }
        }
        taskList.removeAll(taskRemove);
        for (HistoricTaskInstance task : taskList) {
            System.out.println("任务ID:" + task.getId());
            System.out.println("任务名称:" + task.getName());
            System.out.println("任务的创建时间:" + task.getCreateTime());
            System.out.println("任务的办理人:" + task.getAssignee());
            System.out.println("流程实例ID：" + task.getProcessInstanceId());
            System.out.println("执行对象ID:" + task.getExecutionId());
            System.out.println("流程定义ID:" + task.getProcessDefinitionId());
            System.out.println("*****************************************************************************");
        }
        return taskList;
    }

}
